<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>修改密码</title>
    <link rel="stylesheet" href="../layui/css/layui.css" />
    <style>
        body {
            background-image: url('../images/background.jpeg');
            background-size: cover;
            background-position: center;
            background-repeat: no-repeat;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
        }

        .login-container {
            background-color: rgba(255, 255, 255, 0.9);
            padding: 30px;
            border-radius: 15px;
            box-shadow: 0 0 20px rgba(0, 0, 0, 0.2);
            width: 400px;
            max-width: 100%;
        }

        .login-title {
            text-align: center;
            margin-bottom: 20px;
            font-size: 24px;
            color: #333;
        }

        .layui-form-label {
            width: 80px;
            color: #333;
            font-weight: 600;
        }

        .layui-input-inline {
            width: 200px;
        }

        .layui-btn {
            background-color: #009688;
            border-radius: 5px;
            transition: background-color 0.3s ease;
        }

        .layui-btn:hover {
            background-color: #00796b;
        }
    </style>
</head>

<body>
<div class="login-container">
    <div class="login-title">修改密码</div>
    <form class="layui-form" lay-filter="resetPasswordForm">
        <div class="layui-form-item">
            <label class="layui-form-label">用户名</label>
            <div class="layui-input-inline">
                <input type="text" name="username" id="username" autocomplete="off" placeholder="用户名"
                       class="layui-input" lay-verify="required">
            </div>
        </div>
        <!--<div class="layui-form-item">-->
        <!--    <label class="layui-form-label">旧密码</label>-->
        <!--    <div class="layui-input-inline">-->
        <!--        <input type="password" name="oldPassword" id="oldPassword" autocomplete="off" placeholder="旧密码"-->
        <!--               class="layui-input" lay-verify="required">-->
        <!--    </div>-->
        <!--</div>-->
        <div class="layui-form-item">
            <label class="layui-form-label">新密码</label>
            <div class="layui-input-inline">
                <input type="password" name="newPassword" id="newPassword" autocomplete="off" placeholder="新密码"
                       class="layui-input" lay-verify="required">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="resetPasswordForm">确认修改</button>
            </div>
        </div>
    </form>
</div>

<script src="../layui/layui.js"></script>
<script>
    layui.use(['form', 'jquery', 'layer'], function () {
        var form = layui.form,
            $ = layui.jquery,
            layer = layui.layer;

        // 自定义验证规则
        form.verify({
            required: function (value, item) {
                if (!value) {
                    return '该字段不能为空';
                }
            }
        });

        // 监听表单提交事件
        form.on('submit(resetPasswordForm)', function (data) {
            var username = data.field.username;
            // var oldPassword = data.field.oldPassword;
            var newPassword = data.field.newPassword;

            // 发送 AJAX 请求修改密码
            $.ajax({
                type: 'POST',
                url: 'http://59.110.144.74/user/resetPassword',
                data: {
                    username: username,
                    // oldPassword: oldPassword,
                    newPassword: newPassword
                },
                success: function (res) {
                    if (res.success) {
                        layer.msg('密码修改成功，请重新登录', { icon: 1 });
                        setTimeout(function () {
                            window.location.href = 'login_test.html'; // 跳转到登录页面
                        }, 1500);
                    } else {
                        layer.msg(res.message || '密码修改失败，请重试', { icon: 2 });
                    }
                },
                error: function (xhr, status, error) {
                    console.error(error);
                    layer.msg('请求失败，请检查网络或联系管理员', { icon: 2 });
                }
            });

            return false; // 阻止表单默认提交行为
        });
    });
</script>
</body>

</html>
